import { fromJS } from "immutable";
import * as constants from "./constants";

const defaultState = fromJS({
  page: 1,
  totalPage: 0,
  total: 0,
  name: "",
  pageNum: 1,
  pageSize: 10,
  importVisible: false,
  dataSource: [],
  currentPage: 1,
  number: 1,
  totalSize: 0,
  id: 0,
  sendMetaId: Number,
  sendMetaName: "",
  //list界面
  listPage: 1,
  listTotalPage: 0,
  listTotal: 0,
  listPageNum: 1,
  listPageSize: 10,
  listCurrentPage: 1,
  listTotalSize: 0,
  listDataSource: [],
  metaIdsLength: [],
});
const getList = (state, action) => {
  return state.merge({
    dataSource: action.result,
    totalPage: fromJS(action.totalPage),
    total: fromJS(action.total),
    pageSize: fromJS(action.pageSize),
    totalSize: fromJS(action.totalSize),
  });
};
const getInfoList = (state, action) => {
  return state.merge({
    listDataSource: action.result,
    listTotalPage: fromJS(action.totalPage),
    listTotal: fromJS(action.total),
    listPageSize: fromJS(action.pageSize),
    listTotalSize: fromJS(action.totalSize),
  });
};

export default (state = defaultState, action) => {
  switch (action.type) {
    case constants.GET_ALL_LIST:
      return getList(state, action);
    case constants.CHANGE_ADD:
      return state.set("addVisible", action.addVisible);
    case constants.VIEW:
      return state.merge({
        sendMetaId: action.recordId,
        sendMetaName: action.recordName,
        listDataSource: action.result,
        listTotalSize: action.totalSize,
        listTotalPage: action.totalPage,
        listTotal: action.total,
      });
    case constants.EDITOR:
      return state.set("id", action.record);
    case constants.GET_CURRENT_LIST:
      return state.merge({
        dataSource: action.result,
        currentPage: action.pageNum,
      });
    case constants.SEARCH_ITEM:
      return state.merge({
        dataSource: action.result,
        totalSize: action.totalSize,
        totalPage: action.totalPage,
        total: action.total,
        currentPage: 1,
      });
    case constants.CHANGE_PAGE:
      return state.merge({
        currentPage: action.current,
        pageSize: action.pageSize,
      });
    case "change_size":
      return state.set("currentPage", action.pageNum);
    case "clearAll":
      return state.set("dataSource", []);
    case "get_metadatainfo":
      return getInfoList(state, action);
    case "change_info_size":
      return state.set("listCurrentPage", action.pageNum);
    case constants.CHANGE_INFO_PAGE:
      return state.merge({
        listCurrentPage: action.current,
        listPageSize: action.pageSize,
      });
    case constants.GET_CURRENT_INFO_LIST:
      return state.merge({
        listDataSource: action.result,
        listCurrentPage: action.pageNum,
      });
    case "render_length":
      return state.set("metaIdsLength", action.result);
    case constants.SEARCH_INFO_ITEM:
      return state.merge({
        listDataSource: action.result,
        listTotalSize: action.totalSize,
        listTotalPage: action.totalPage,
        listTotal: action.total,
        listCurrentPage: 1,
      });
    case "back_first":
      return state.set("listCurrentPage", 1);
    case "back_type_first":
      return state.set("currentPage", 1);
    default:
      return state;
  }
};
